home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / dirut / attribcr.zip / ATTRIBCR.ASM next >
Assembly Source File  |  1986-02-21  |  16KB  |  533 lines

  1.     PAGE    62,132
  2.     TITLE    ATTRIB - a program to alter file attributes
  3.     NAME    ATTRIB
  4. ; attrib.asm  3 Feb 86  Craig Milo Rogers at USC/ISI
  5. ;    I received this program from net.sources in response to an
  6. ; inquiry I sent to Info-IBMPC.  Unfortunately, the program fell
  7. ; short of my goals:  it didn't support the Archive bit.  However,
  8. ; the program was nicely structured and readily extended.
  9. ;
  10. ;    I also made a couple of other minor changes.  I changed the
  11. ; command parser to make '.*' the default when the file name extension
  12. ; is not specified.  I changed the command parser to accept ";" as a
  13. ; delimiter.  I changed the command parser to accept multiple
  14. ; attributes.  In the case of incompatible attributes the last one
  15. ; wins.
  16. ;
  17. ;    The user should be aware that there are some other
  18. ; differences between this program and the DOS 3.10 ATTRIB command.
  19. ; The DOS ATTRIB command lists the entire drive and path when
  20. ; listing files, while this program lists only as much of the
  21. ; path as the user specified.
  22. ;
  23. ; This program was written by Bob Eager, Herne Bay, England.
  24. ; It is placed in the public domain. No part of this program
  25. ; may be copied and sold for gain.
  26. ;
  27. ; There isn't really much in the way of documentation. ATTRIB
  28. ; works like the DOS 3 command of the same name (except that it
  29. ; is a lot smaller and also DOES a lot MORE!)
  30. ;
  31. ; For those unfamiliar with that:
  32. ;    ATTRIB filename     -   reports file attributes as single letters
  33. ;                            (H, S, R, A)
  34. ;    ATTRIB +x filename  -   sets the x attribute (H, S, R or A)
  35. ;    ATTRIB -x filename  -   unsets the x attribute (H, S, R ar A)
  36. ;
  37. ;  The filename may be a wildcard.  Multiple attributes may be specified.
  38. ;
  39. ; Inverse attributes are provided as a friendly touch:
  40. ;
  41. ;      Hidden   <==>   Visible
  42. ;      System   <==>   User
  43. ;      Read     <==>   Write
  44. ;      Archive  <==>   Backed-up
  45. ;
  46. ; So +R is the same as -W, etc.
  47. ;
  48. ;   Enjoy!
  49. ;
  50.     .SALL
  51. ;
  52. ; Values for exit status:
  53. ;    0 - Success
  54. ;    1 - Parameter too long
  55. ;    2 - Incorrect DOS version
  56. ;    3 - File does not exist
  57. ;    4 - Invalid attribute letter
  58. ;
  59. ; Constants
  60. ;
  61. TAB    EQU    09H            ; Tabulate
  62. LF    EQU    0AH            ; Linefeed
  63. CR    EQU    0DH            ; Carriage return
  64. PTHSEP1    EQU    '\'            ; Normal path separator
  65. PTHSEP2    EQU    '/'            ; Alternate path separator
  66. ;
  67. STDOUT    EQU    1            ; Standard output handle
  68. STDERR    EQU    2            ; Standard error handle
  69. MAXFNAM    EQU    63            ; Maximum length of filename
  70. ;
  71. ; Macro definitions
  72. ;
  73. MSG    MACRO    X,Y            ;; define a message
  74. MES&X    DB    Y
  75. LMES&X    EQU    $-MES&X
  76.     ENDM
  77. ;
  78. $MSG    MACRO    X            ;; display a message on standard error
  79.     MOV    BX,STDERR        ;; file handle
  80.     MOV    DX,OFFSET MES&X        ;; location of message
  81.     MOV    CX,LMES&X        ;; length of message
  82.     MOV    AH,40H            ;; write function
  83.     INT    21H
  84.     ENDM
  85. ;
  86. CSEG    SEGMENT
  87. ;
  88.     ORG    100H
  89. ;
  90.     ASSUME    CS:CSEG,DS:CSEG,ES:CSEG,SS:CSEG
  91. ;
  92. MAIN    PROC    FAR
  93.     JMP    MAIN10
  94. ;
  95.     SUBTTL    Data areas
  96.     PAGE+
  97. ;
  98. ; Storage
  99. ;
  100. ATT    DW    ?            ; Mask and bits specified by parameter
  101. FIRST    DB    ?            ; Used for 'find first' calls
  102. PARPTR    DW    ?            ; Pointer to next parameter
  103. PSIZE    DW    ?            ; Size of path part of filename
  104. DISP    DB    ?,?,?,?,'    '        ; Attribute display string
  105. LDISP    EQU    $-DISP            ; Length of DISP
  106. FNAME    DB    MAXFNAM+1 DUP (?)    ; Extra byte for terminator
  107. DTA    DB    43 DUP (?)        ; Alternate disk transfer area
  108. ;
  109. ; Table of attribute settings
  110. ;
  111. ; The table ATTC is a list of valid attribute letters
  112. ; The table ATTON corresponds one-to-one with ATTC for setting an attribute
  113. ; The table ATTOFF corresponds one-to-one with ATTC for clearing an attribute
  114. ; ATTON and ATTOFF use the high byte of each entry as a mask to clear
  115. ; unwanted bits, and the low byte to indicate bits to be set.
  116. ;
  117. ATTC    DB    'RWHVSUAB'
  118. NATT    EQU    $-ATTC
  119. ATTON    DW    0FE01H,0FE00H,0FD02H,0FD00H,0FB04H,0FB00H,0DF20H,0DF00H
  120. ATTOFF    DW    0FE00H,0FE01H,0FD00H,0FD02H,0FB00H,0FB04H,0DF00H,0DF20H
  121. ;
  122. CRBUF    DB    CR,LF
  123. ;
  124.     DW    32 DUP (?)        ; Stack
  125. STACK    LABEL    WORD
  126. ;
  127. ; Messages
  128. ;
  129.     MSG    1,<'Parameter too long',CR,LF>
  130.     MSG    2,<'Filename too long - '>
  131.     MSG    3,<'Incorrect DOS version',CR,LF>
  132.     MSG    4,<'No matching file found for '>
  133.     MSG    5,<'Invalid attribute letter',CR,LF>
  134. ;
  135.     SUBTTL    Main code
  136.     PAGE+
  137. ;
  138. MAIN10:    MOV    SP,OFFSET STACK        ; set up stack
  139. ;
  140.     MOV    AH,30H
  141.     INT    21H            ; get DOS version
  142.     XCHG    AH,AL            ; make more useful form
  143.     CMP    AX,0200H        ; version 2 at least?
  144.     JAE    MAIN20            ; j if so - OK
  145.     $MSG    3            ; "Incorrect DOS version"
  146.     MOV    AL,2            ; exit status
  147.     JMP    SHORT MAIN60        ; join common exit code
  148. ;
  149. MAIN20:    MOV    DX,OFFSET DTA        ; set alternate DTA to avoid...
  150.     MOV    AH,1AH            ; ...overwriting parameters
  151.     INT    21H
  152.     MOV    PARPTR,81H        ; start of parameter area
  153.     CALL    GETFLAGS        ; get any attribute flags
  154.     JC    MAIN60            ; j if error (unrecognised flag)
  155. MAIN30:    CALL    GETPAR            ; read next parameter item
  156.     JC    MAIN50            ; j if no more parameters
  157.     CALL    DOTSTR            ; add '.*' if needed
  158.     MOV    BYTE PTR FIRST,0    ; clear 'find first' flag
  159. MAIN40:    CALL    GETNAM            ; get next filename
  160.     JC    MAIN30            ; j if no more names
  161.     CALL    DOATTR            ; handle attribute
  162.     JC    MAIN60            ; j if error (status in AL)
  163.     JMP    MAIN40            ; see if more to do
  164. ;
  165. MAIN50:    XOR    AL,AL            ; zero status
  166. ;
  167. MAIN60:    MOV    AH,4CH            ; exit
  168.     INT    21H
  169. MAIN    ENDP
  170. ;
  171.     SUBTTL    Get attribute flag from command line
  172.     PAGE
  173. ;
  174. ; This routine reads an attribute flag and its sense from the command line.
  175. ; Attribute flags start with '+' or '-' and continue with a letter.
  176. ; Possible letters are:  R  -  read only
  177. ;                        W  -  read and write (inverse of read only)
  178. ;                        H  -  hidden
  179. ;                        V  -  visible (inverse of hidden)
  180. ;                        S  -  system
  181. ;                        U  -  user (inverse of system)
  182. ;             A  -  archive needed
  183. ;             B  -  backed-up (inverse of archive-needed)
  184. ;
  185. ; On entry:  PARPTR points to the first byte of the parameter string
  186. ;
  187. ;  On exit:  PARPTR has been updated past the attribute specification if
  188. ;            one was found
  189. ;
  190. ; Return is made with ATT high containing a mask to remove any unwanted bits,
  191. ; and with ATT low containing any additional bits to be set in the attributes.
  192. ; The value ATT=0FF00H is returned if no attribute flag was found, and carry
  193. ; is set if an unrecognised attribute letter was encountered.
  194. ;
  195. GETFLAGS PROC    NEAR
  196.     MOV    ATT,0FF00H        ; This mask leaves the attributes
  197.                     ; unchanged.
  198.     CLD                ; autoincrement
  199.     MOV    SI,PARPTR        ; point to start of parameter list
  200. GETF10:    LODSB                ; skip leading spaces...
  201.     CMP    AL,' '
  202.     JE    GETF10
  203.     CMP    AL,TAB            ; ...and tabs
  204.     JE    GETF10
  205.     CMP    AL,'+'            ; valid sense?
  206.     JE    GETF20            ; j if so
  207.     CMP    AL,'-'            ; other sense?
  208.     JE    GETF20            ; j if so
  209. GETF15:    DEC    SI            ; Backup the parameter pointer.
  210.     MOV    PARPTR,SI        ; Save the pointer for later use.
  211.     CLC                ; indicate success
  212.     RET                ; return with carry clear
  213. ;
  214. GETF20:    MOV    DL,AL            ; save sense
  215.     LODSB                ; get attribute letter
  216. GETF25:    CALL    UPPER            ; convert to upper case
  217.     MOV    BX,OFFSET ATTC        ; list of valid letters
  218.     MOV    CX,NATT            ; count of valid letters
  219.     XOR    DI,DI            ; pointer into letter table
  220. GETF30:    CMP    AL,[BX+DI]        ; found the letter?
  221.     JE    GETF40            ; j if so
  222.     INC    DI            ; point to next possibility
  223.     LOOP    GETF30            ; keep trying
  224.     $MSG    5            ; "Invalid attribute letter"
  225.     MOV    AL,4            ; exit status
  226.     STC                ; indicate error
  227.     RET
  228. ;
  229. GETF40:    MOV    BX,OFFSET ATTON        ; assume sense 'on'
  230.     CMP    DL,'-'
  231.     JNE    GETF50            ; j if sense 'on' required
  232.     MOV    BX,OFFSET ATTOFF    ; else use different table
  233. GETF50:    ADD    DI,DI            ; form word offset
  234.     MOV    AX,[BX+DI]        ; get mask and attribute word
  235.     MOV    BX,ATT            ; Get the previous mask.
  236.     AND    BL,AH            ; Clear incompatible bits.
  237.     AND    AH,BH            ; Merge the bits-to-keep mask.
  238.     OR    AL,BL            ; Merge the bits-to-set mask.
  239.     MOV    ATT,AX            ; Save the resultant mask.
  240.  
  241. GETF60:    LODSB                ; Get the next command byte.
  242.     CMP    AL,' '            ; Space means end of these flags.
  243.     JE    GETF10            ; Jump to look for next flags.
  244.     CMP    AL,TAB            ; Tab also means end of these flags.
  245.     JE    GETF10            ; Jump to look for next flags.
  246.     CMP    AL,CR            ; CR is premature, treat it
  247.     JE    GETF15            ; as the end of the flags.
  248.     CMP    AL,'+'            ; Plus is another set of flags.
  249.     JE    GETF20            ; Go parse the flags.
  250.     CMP    AL,'-'            ;